class ListNode:
    def __init__(self,val=0,next=None):
        self.val=val
        self.next=next
head=ListNode(1,None)
head.next=ListNode(2,None)
# head.next.next=ListNode(3,None)
# head.next.next.next=ListNode(4,None)
# head.next.next.next.next=ListNode(5,None)
def removeNthFromEnd(head,n):
    if head.next==None and n==1:
        head=None
        return head
    head_new=ListNode()
    head_new.next=head
    l_pointer,r_pointer=head_new,head_new
    i=0
    while r_pointer.next!=None:
        #往后一直走
        r_pointer=r_pointer.next
        if i>=n:
            l_pointer=l_pointer.next
        i+=1
    if n==1:
        l_pointer.next=None
    else:
        l_pointer.next=l_pointer.next.next
    p_pointer=head_new
    while p_pointer!=None:
        print(p_pointer.val)
        p_pointer=p_pointer.next
    return head_new.next
removeNthFromEnd(head,2)
